# get git hash
macro(get_git_hash _git_hash _format)
    find_package(Git QUIET)
    if(GIT_FOUND)
      execute_process(
        COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%${_format}
        OUTPUT_VARIABLE ${_git_hash}
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
        WORKING_DIRECTORY
          ${CMAKE_CURRENT_SOURCE_DIR}
        )
    endif()
endmacro()

# get git branch
macro(get_git_branch _git_branch)
    find_package(Git QUIET)
    if(GIT_FOUND)
      execute_process(
        COMMAND ${GIT_EXECUTABLE} symbolic-ref --short -q HEAD
        OUTPUT_VARIABLE ${_git_branch}
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
        WORKING_DIRECTORY
          ${CMAKE_CURRENT_SOURCE_DIR}
        )
    endif()
endmacro()

set(GIT_COMMIT_HASH     "unknown")
set(GIT_AUTHOR_NAME     "unknown")
set(GIT_AUTHOR_EMAIL    "unknown")
set(GIT_AUTHOR_DATE     "unknown")
set(GIT_COMMITTER_NAME  "unknown")
set(GIT_COMMITTER_EMAIL "unknown")
set(GIT_COMMITTER_DATE  "unknown")
set(GIT_SUBJECT         "unknown")

get_git_hash(GIT_COMMIT_HASH     "H")
get_git_hash(GIT_AUTHOR_NAME     "an")
get_git_hash(GIT_AUTHOR_EMAIL    "ae")
get_git_hash(GIT_AUTHOR_DATE     "ai")
get_git_hash(GIT_COMMITTER_NAME  "cn")
get_git_hash(GIT_COMMITTER_EMAIL "ce")
get_git_hash(GIT_COMMITTER_DATE  "ci")
get_git_hash(GIT_SUBJECT         "s")

set(GIT_BRANCH "unknown")
get_git_branch(GIT_BRANCH)

